home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / netlib / popen.c < prev    next >
C/C++ Source or Header  |  1994-05-02  |  10KB  |  400 lines

  1. RCS_ID_C="$Id: popen.c,v 1.2 1994/05/02 19:35:42 jraja Exp $";
  2. /*
  3.  * popen.c -- Unix comaptible popen() and pclose()
  4.  *
  5.  * Author: Rick Schaeffer <ricks@isc-br.isc-br.com>
  6.  *
  7.  * $Log: popen.c,v $
  8.  * Revision 1.2  1994/05/02  19:35:42  jraja
  9.  * Fixed autodoc entry name.
  10.  *
  11.  * Revision 1.1  1994/04/04  01:31:48  jraja
  12.  * Initial revision
  13.  *
  14.  * Revision 1.1  1993/11/28  20:43:15  jraja
  15.  * Initial revision
  16.  *
  17.  * Revision 1.3  1993/10/05  21:40:06  alph
  18.  * *** empty log message ***
  19.  *
  20.  * Revision 1.2  1993/10/05  21:38:19  alph
  21.  * *** empty log message ***
  22.  *
  23.  * Revision 1.1  1993/01/19  14:07:12  heinz
  24.  * Initial revision
  25.  *
  26.  * Revision 1.3  93/01/03  11:57:50  heinz
  27.  * All new popen.c, Cleans up, works nice, it's just fine :-)
  28.  *
  29.  * Revision 1.2  93/01/02  22:27:55  heinz
  30.  * Major cleanup for SAS/C 6.1, childprocess() is now clean!!
  31.  * There is still the DOSBase passing, though.
  32.  *
  33.  * Revision 1.1  92/12/26  16:55:24  heinz
  34.  * Initial revision
  35.  *
  36.  *
  37.  */
  38.  
  39. /****** net.lib/popen *******************************************************
  40.     NAME
  41.         popen, pclose - initiate I/O to/from a process
  42.  
  43.     SYNOPSIS
  44.         #include <stdio.h>
  45.  
  46.         FILE *popen(command, type)
  47.         char *command, *type;
  48.  
  49.         pclose(stream)
  50.         FILE *stream;
  51.  
  52.     DESCRIPTION
  53.         The arguments to popen are pointers to null-terminated
  54.         strings containing respectively a command line and an
  55.         I/O mode, either "r" for reading or "w" for writing.  It
  56.         creates a pipe between the calling process and the command
  57.         to be executed.  The value returned is a stream pointer that
  58.         can be used (as appropriate) to write to the standard input
  59.         of the command or read from its standard output.
  60.  
  61.         A stream opened by popen **MUST** be closed by pclose, which
  62.         waits for the associated process to terminate and returns
  63.         the exit status of the command.
  64.  
  65.         Because stdio files are shared, a type "r" command may be
  66.         used as an input filter, and a type "w" as an output filter.
  67.  
  68.     DIAGNOSTICS
  69.         Popen returns a null pointer if files or processes cannot be
  70.         created.
  71.  
  72.         Pclose returns -1 if stream is not associated with a
  73.         `popened' command.
  74.  
  75.     AUTHOR
  76.         Original version by Rick Schaeffer <ricks@isc-br.isc-br.com>
  77. */
  78.  
  79. #include <stdio.h>
  80. #include <stdlib.h>
  81. #include <string.h>
  82. #include <stdarg.h>
  83.  
  84. #include <exec/types.h>
  85. #include <exec/memory.h>
  86. #include <dos/dos.h>
  87. #include <dos/dosextens.h>
  88. #include <dos/record.h>
  89. #include <dos/dostags.h>
  90.  
  91. #include <proto/exec.h>
  92. #include <proto/dos.h>
  93. #include <clib/alib_protos.h>
  94.  
  95. #include <errno.h>
  96.  
  97. #define NOTDEF
  98.  
  99. extern char *mktemp(char *);
  100.  
  101. struct POmsg {
  102.     struct Message  POm;
  103.     int     rc;
  104.     char        *cmd;
  105.     struct Library  *DOSBase;
  106.     };
  107.  
  108.  
  109. struct pstruct {
  110.     FILE    *fptr;
  111.     struct POmsg    childmsg;
  112.     };
  113.  
  114. #define MAXPIPES    6
  115. static struct pstruct poarray[MAXPIPES];
  116.  
  117. static int childprocess(void);
  118.  
  119. FILE *popen(const char *cmd, const char *mode)
  120. {
  121.     static char tempname[] = "pipe:pXXX.XXX";
  122.     char        *pname,redir[20];
  123.     short       i;
  124.     int         pmode;
  125.     struct pstruct  *poptr;
  126.     BPTR            pfd;
  127.     struct Process  *child;
  128.     struct CommandLineInterface *cli;
  129.     BPTR Binfh, Boutfh;
  130.     ULONG closeinp, closeoutp;
  131.     ULONG stacksize;
  132.     struct Process *thistask;
  133.  
  134.     /* First, get pointers to our process and cli structs */
  135.     thistask = (struct Process *) FindTask(NULL);
  136.     cli = Cli();
  137.     poptr = NULL;
  138.  
  139.     /* now find an open pipe (we currently only allow 6 simultaneously
  140.        open pipes) */
  141.     for (i=0; i<MAXPIPES; i++) {
  142.         if (poarray[i].fptr == NULL) {
  143.             poptr = &poarray[i];
  144.             break;
  145.             }
  146.         }
  147.     if (poptr == NULL) {
  148.         fprintf(stderr,"popen: Unable to find an open pipe\n");
  149.         errno = EMFILE;
  150.         return(NULL);
  151.         }
  152.     if (strcmp(mode,"r") == 0)
  153.         pmode = MODE_NEWFILE;
  154.     else if (strcmp(mode,"w") == 0)
  155.         pmode = MODE_OLDFILE;
  156.     else {
  157.         fprintf(stderr,"popen: Mode must be 'r' or 'w'\n");
  158.         errno = EINVAL;
  159.         return(NULL);
  160.         }
  161.  
  162.     /* Try to make a guaranteed unique file name for the pipe */
  163.     strcpy(redir,tempname);
  164.     redir[5] = 'a' + i;
  165.  
  166.     pname = mktemp(redir);            /* set up a pipe: file name */
  167.  
  168.     /* Now get the child's stack and priority set up */
  169.     if (cli)
  170.         stacksize = cli->cli_DefaultStack << 2;
  171.     else
  172.         stacksize = thistask->pr_StackSize;
  173.  
  174.     /* Open the side of the pipe for the child */
  175.     pfd = Open(pname,pmode);
  176.     if (pfd == 0) {
  177.         errno = __io2errno(_OSERR = IoErr());
  178.         fprintf(stderr,"popen: Unable to open pipe file\n");
  179.         return(NULL);
  180.         }
  181.  
  182.     /* set up the tags for the new process */
  183.     if (pmode == MODE_NEWFILE) {
  184.         Binfh     = (Tag) Input();
  185.         Boutfh    = (Tag) pfd;
  186.         closeinp  = FALSE;
  187.         closeoutp = TRUE;
  188.         }
  189.     else {
  190.         Binfh     = (Tag) pfd;
  191.         Boutfh    = (Tag) Output();
  192.         closeinp  = TRUE;
  193.         closeoutp = FALSE;
  194.         }
  195.  
  196.     /* create the command.  since the "System" function runs through
  197.        the default shell, we need to tell it not to fail so that we
  198.        ALWAYS get back the exit status.  This wouldn't be necessary
  199.        if the CLI created by the System function inherited the parent's
  200.        FAILAT level
  201.     */
  202.     poptr->childmsg.cmd = malloc(strlen(cmd) + 15);
  203.     strcpy(poptr->childmsg.cmd,"failat 9999\n");
  204.     strcat(poptr->childmsg.cmd,cmd);
  205.  
  206.     /* Create a port that we can get the child's exit status through */
  207.     poptr->childmsg.POm.mn_ReplyPort = CreateMsgPort();
  208.     poptr->childmsg.POm.mn_Node.ln_Type = NT_MESSAGE;
  209.     poptr->childmsg.POm.mn_Node.ln_Pri = 0;
  210.     if (poptr->childmsg.POm.mn_ReplyPort == 0) {
  211.         fprintf(stderr,"popen: Couldn't create message port\n");
  212.         errno = ENOMEM;
  213.         return(NULL);
  214.         }
  215.  
  216.     /* Now we can start the new process.  NOTE: this is actually going
  217.        to create a process consisting ONLY of the function "childprocess"
  218.        which can be seen below.  childprocess() then runs the command
  219.        passed in the startup message.
  220.     */
  221.     child = CreateNewProcTags(
  222.         NP_Entry,   (Tag) childprocess,
  223.         NP_Input,   Binfh,
  224.         NP_Output,  Boutfh,
  225.         NP_CloseInput,  closeinp,
  226.         NP_CloseOutput, closeoutp,
  227.         NP_StackSize,   stacksize,
  228.         NP_Cli,     TRUE,
  229.         TAG_DONE
  230.         );
  231.  
  232.     poptr->childmsg.DOSBase = (struct Library *)DOSBase;
  233.  
  234.     /* now pass the child the startup message */
  235.     PutMsg(&child->pr_MsgPort,(struct Message *) &poptr->childmsg);
  236.  
  237.     /* Now open our side of the pipe */
  238.     poptr->fptr = fopen(pname,mode);
  239.     if (poptr->fptr == NULL) {
  240.         fprintf(stderr,"popen: Unable to open pipe file %s\n",pname);
  241.         DeleteMsgPort(poptr->childmsg.POm.mn_ReplyPort);
  242.         return(NULL);
  243.         }
  244.     return(poptr->fptr);
  245. }
  246.  
  247. FILE *popenl(const char *arg0, ...)
  248. {
  249.     va_list ap;
  250.     char argbuf[512], *mode;
  251.  
  252.     strcpy(argbuf, arg0);
  253.     va_start(ap, arg0);
  254.     while(1)
  255.     {
  256.         char *s = va_arg(ap, char *);
  257.  
  258.         if(s == NULL)
  259.         {
  260.         strcat(argbuf, "\n");
  261.         break;
  262.         } /* if */
  263.  
  264.         strcat(argbuf, " ");
  265.  
  266.         if(strchr(s, ' '))
  267.         {
  268.         strcat(argbuf, "\"");
  269.         strcat(argbuf, s);
  270.         strcat(argbuf, "\"");
  271.         }
  272.         else
  273.         {
  274.         strcat(argbuf, s);
  275.         } /* if */
  276.     }
  277.     mode = va_arg(ap, char *);
  278.     va_end(ap);
  279.  
  280.     return(popen(argbuf, mode));
  281.  
  282. } /* popenl */
  283.  
  284. int pclose(FILE *fptr)
  285. {
  286.     short       i;
  287.  
  288.     /* Figure out which pipe we used for this file */
  289.     for (i=0; i<MAXPIPES; i++)
  290.         if (poarray[i].fptr == fptr)
  291.             break;
  292.     if (i >= MAXPIPES) {
  293.         fprintf(stderr,"popen: DISASTER...couldn't find file pointer in pclose\n");
  294.         exit(1);
  295.         }
  296.  
  297.     /* close the file */
  298.     fclose(fptr);
  299.  
  300.     /* now wait for the exit status */
  301.     WaitPort(poarray[i].childmsg.POm.mn_ReplyPort);
  302.     poarray[i].fptr = NULL;
  303.  
  304.     /* clean things up */
  305.     DeletePort(poarray[i].childmsg.POm.mn_ReplyPort);
  306.     free(poarray[i].childmsg.cmd);
  307.     return(poarray[i].childmsg.rc);
  308. }
  309.  
  310. /* SAS/C autoinitialization for cleanup! */
  311. void __stdargs _STD_4000_popen(void)
  312. {
  313.     short i;
  314.  
  315.     /* Close all the open pipes! */
  316.     for(i=0; i<MAXPIPES; i++)
  317.     {
  318.         if(poarray[i].fptr)
  319.         {
  320.             pclose(poarray[i].fptr);
  321.         } /* if */
  322.     } /* for */
  323.  
  324. } /* _STD_4000_popen */
  325.  
  326. #ifdef NOTDEF
  327.  
  328. char *mktemp(char * template)
  329. {
  330.     register char *cp;
  331.     register unsigned long val;
  332.  
  333.     cp = template;
  334.     cp += strlen(cp);
  335.     for (val = (unsigned long) FindTask(0L) ; ; )
  336.         if (*--cp == 'X') {
  337.             *cp = val%10 + '0';
  338.             val /= 10;
  339.         } else if (*cp != '.')
  340.             break;
  341.  
  342.     if (*++cp != 0) {
  343.         *cp = 'A';
  344.         while (access(template, 0) == 0) {
  345.             if (*cp == 'Z') {
  346.                 *template = 0;
  347.                 break;
  348.             }
  349.             ++*cp;
  350.         }
  351.     } else {
  352.         if (access(template, 0) == 0)
  353.             *template = 0;
  354.     }
  355.     return template;
  356. }
  357.  
  358. #endif
  359.  
  360. /* WATCH OUT! This only works without __saveds because of the special
  361.    SAS/C 6.1 tricks I use! Check the output with omd! */
  362. static int __interrupt childprocess(void)
  363. {
  364.     struct ExecBase *SysBase = *((struct ExecBase **)4);
  365.     struct Library *DOSBase;
  366.     struct Process  *me;
  367.     struct POmsg    *startupmsg;
  368.     int             i = RETURN_FAIL;
  369.  
  370.     /* find our process structure */
  371.     me = (struct Process *) FindTask(NULL);
  372.  
  373.     /* Wait for the parent to kick us off */
  374.     WaitPort(&me->pr_MsgPort);
  375.  
  376.     /* Get the command to execute */
  377.     startupmsg = (struct POmsg *) GetMsg(&me->pr_MsgPort);
  378.  
  379.     DOSBase = startupmsg->DOSBase;
  380.  
  381.     if(DOSBase)
  382.     {
  383.         /* Now run the command.  stdin and stdout are already set up */
  384.         i = SystemTags(startupmsg->cmd,
  385.                SYS_UserShell, 1,
  386.                TAG_DONE);
  387.     } /* if */
  388.  
  389.     if(i > 0)
  390.     {
  391.         /* UNIX compatibility ... */
  392.         i <<= 8;
  393.     } /* if */
  394.  
  395.     startupmsg->rc = i;
  396.     /* pass the exit code back to the parent */
  397.     ReplyMsg((struct Message *) startupmsg);
  398.     return(0);
  399. }
  400.